<template>
  <div class="menu-container">
    <el-row class="heardadd">
      <el-button
        type="success"
        icon="el-icon-edit"
        class="addbtn"
        @click="isaddShow"
        >添加菜单</el-button
      >
    </el-row>
    <!-- 列表区域 -->
    <el-table
      :data="menulist"
      style="width: 100%"
      row-key="id"
      :tree-props="{ children: 'points' }"
      default-expand-all
    >
      <el-table-column prop="title" label="标题" width="180"> </el-table-column>
      <el-table-column prop="code" label="权限点代码" width="180">
      </el-table-column>
      <el-table-column label="操作">
        <template v-slot="scope">
          <el-button size="mini" @click="editClick(scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="deleteClick(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 列表区域 -->
    <!-- 添加区域 -->

    <el-dialog title="创建菜单" :visible.sync="isShow">
      <el-form :model="formlist">
        <el-form-item label="权限组名称" :label-width="formLabelWidth">
          <el-radio-group v-model="formlist.is_point">
            <el-radio :value="formlist.is_point" :label="false" @change="jjj"
              >菜单</el-radio
            >
            <el-radio :value="formlist.is_point" :label="true" @change="jjj"
              >权限点</el-radio
            >
          </el-radio-group>
        </el-form-item>

        <el-form-item label="权限组名称" :label-width="formLabelWidth">
          <!-- 使用插件 treeselect -->
          <!-- 权限点  -->
          <treeselect
            v-if="formlist.is_point === true"
            :show-count="true"
            :disable-branch-nodes="true"
            :options="menulist"
            :flat="true"
            :normalizer="normalizer1"
            @select="selectDepart"
          >
            ></treeselect
          >
          <!-- 菜单  v-else         -->
          <treeselect
            v-else
            :show-count="true"
            :disable-branch-nodes="true"
            :options="menulist"
            :flat="true"
            :normalizer="normalizer2"
            @select="selectDepart"
          >
            ></treeselect
          >
        </el-form-item>
        <el-form-item label="权限代码" :label-width="formLabelWidth">
          <el-input v-model="formlist.code" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="权限标题" :label-width="formLabelWidth">
          <el-input v-model="formlist.title" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="isShow = false">取 消</el-button>
        <el-button type="primary" @click="addClick">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 添加区域 -->
    <!-- 编辑区域 -->
    <el-dialog title="编辑菜单" :visible.sync="iseditShow">
      <el-form :model="editformlist">
        <el-form-item label="权限组名称" :label-width="formLabelWidth">
          <el-radio-group v-model="formlist.is_point">
            <el-radio :value="editformlist.is_point" :label="true" disabled
              >菜单</el-radio
            >
            <el-radio :value="editformlist.is_point" :label="false" disabled
              >权限点</el-radio
            >
          </el-radio-group>
        </el-form-item>

        <el-form-item label="权限组名称" :label-width="formLabelWidth">
          <!-- 使用插件 treeselect -->

          <!-- 菜单  v-else         -->
          <treeselect
            :show-count="true"
            :disable-branch-nodes="true"
            :options="menulist"
            :flat="true"
            :normalizer="normalizer2"
          >
            ></treeselect
          >
        </el-form-item>
        <el-form-item label="权限代码" :label-width="formLabelWidth">
          <el-input v-model="editformlist.code" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="权限标题" :label-width="formLabelWidth">
          <el-input v-model="editformlist.title" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="iseditShow = false">取 消</el-button>
        <el-button type="primary" @click="editOnClick">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 编辑区域 -->
    <!-- 删除 -->
    <el-dialog title="提示" :visible.sync="deletedialogVisible" width="30%">
      <span>是否确认删除</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="deletedialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="deleteOclick">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 删除 -->
  </div>
</template>

<script>
import { addmenu, tianjiamenu, editmenu, deletmenu } from '@/api/default'
// import the component
import Treeselect from '@riophae/vue-treeselect'
// import the styles
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
export default {
  name: 'Menu',

  components: { Treeselect },
  data() {
    return {
      menulist: [],
      formlist: {
        code: '',
        id: '',
        is_point: true,
        pid: '',
        title: ''
      },
      // 编辑表单
      editformlist: {
        code: '',
        id: '',
        is_point: true,
        pid: '',
        title: ''
      },
      // 删除id
      deletaid: '',
      normalizer1(node) {
        return {
          id: node.id,
          label: node.title,
          children: node.childs,
          isDisabled: !node.pid
        }
      },
      normalizer2(node) {
        return {
          id: node.id,
          label: node.title,
          children: node.childs
        }
      },
      isShow: false,
      iseditShow: false,
      deletedialogVisible: false,

      formLabelWidth: '7.5rem'
    }
  },
  created() {
    this.getmenulist()
  },
  methods: {
    async getmenulist() {
      const res = await addmenu()
      // console.log(res)
      if (res.status !== 200) {
        return this.$message.error('获取菜单信息数据失败')
      }
      this.menulist = res.data

      // this.menulist.forEach((item) => {
      //   if (item.child s) {
      //     item.childs.forEach((item) => {
      //       if (item.points) {
      //         item.childs = item.points
      //         console.log(item.childs)
      //       }
      //     })
      //   }
      // })

      // console.log(this.menulist)
      this.menulist.forEach((item) => {
        item.points = item.childs
      })
    },
    isaddShow() {
      this.isShow = true
      console.log(this.formlist)
    },
    // jjj(q) {
    //   console.log(q)

    //   console.log(this.menulist)
    // },
    // 确认 发送get请求
    async addClick() {
      console.log(this.formlist)

      const res = await tianjiamenu({
        is_point: this.formlist.is_point, //  是否权限点
        pid: this.formlist.pid, //  父 id
        code: this.formlist.code, //  菜单代码 全局唯一
        title: this.formlist.title, //  菜单标题
        id: this.formlist.id
      })

      // console.log(res)
      if (res.status !== 200) {
        this.$message.error('添加信息失败,请重试')
      }
      // 添加成功渲染页面
      this.getmenulist()
      // 关闭窗口
      this.isShow = false
    },

    selectDepart(val) {
      console.log('selectDepart', val)
      this.formlist.id = val.id
      this.formlist.pid = val.pid
      this.formlist.code = val.code
      this.formlist.title = val.title
    },

    // 编辑按钮 渲染视图
    editClick(data) {
      this.iseditShow = true
      // 得到数据展示视图
      console.log(data)
      this.editformlist.code = data.code
      this.editformlist.id = data.id
      this.editformlist.is_point = data.is_point || false
      this.editformlist.pid = data.pid
      this.editformlist.title = data.title
      console.log(this.editformlist)
    },
    // 发送编辑后的信息 渲染视图
    async editOnClick() {
      const res = await editmenu(this.editformlist.id, {
        code: this.editformlist.code,
        is_point: this.editformlist.is_point,
        pid: this.editformlist.pid,
        title: this.editformlist.title,
        id: this.editformlist.id
      })
      // console.log(res)
      if (res.status !== 200) {
        this.$message.error('编辑失败')
      }
      // 添加成功渲染页面
      this.iseditShow = false
      this.getmenulist()
    },
    // 显示删除
    deleteClick(data) {
      this.deletedialogVisible = true
      this.deletaid = data
    },
    // 删除
    async deleteOclick() {
      this.deletedialogVisible = false
      const res = await deletmenu(this.deletaid.id)
      console.log(this.deletaid)
      if (res.status === 200) {
        this.$message.success('删除成功')
        this.getmenulist()
      }
    }
  }
}
</script>

<style lang="less" scoped>
.heardadd {
  position: relative;
  margin-bottom: 3.125rem;
}
.addbtn {
  position: absolute;
  right: 0.625rem;
}
</style>
